package io.github.yidasanqian.controller;

import io.github.yidasanqian.content.service.ContentCategoryService;
import io.github.yidasanqian.content.service.ContentService;
import io.github.yidasanqian.dto.ContentCategoryDTO;
import io.github.yidasanqian.dto.ContentDTO;
import io.github.yidasanqian.vo.ContentCategoryVO;
import io.github.yidasanqian.vo.ContentVO;
import io.github.yidasanqian.vo.EasyUIVO;
import io.github.yidasanqian.vo.ResultVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Linyu Chen
 */
@RequestMapping("content")
@Controller
public class ContentController {

    @Resource
    private ContentService contentService;

    @Resource
    private ContentCategoryService contentCategoryService;

    @PostMapping("/category/create")
    @ResponseBody
    public ResultVO addContentCategoryNode(Long parentId, String name) {
        int row = contentCategoryService.addContentCategoryNode(parentId, name);
        return ResultVO.buildSuccessResult(0, "success", row);
    }

    @PostMapping("/category/delete")
    @ResponseBody
    public ResultVO deleteContentCategoryNode(Long id) {
        int row = contentCategoryService.deleteContentCategoryNode(id);
        return ResultVO.buildSuccessResult(0, "success", row);
    }

    @PostMapping("/category/update")
    @ResponseBody
    public ResultVO updateContentCategoryNode(Long id, String name) {
        int row = contentCategoryService.updateContentCategoryNode(id, name);
        return ResultVO.buildSuccessResult(0, "success", row);
    }

    @GetMapping("/category/list")
    @ResponseBody
    public List<ContentCategoryVO> queryGoodsCategoryNodeList(@RequestParam(value = "id", defaultValue = "0") Long parentId) {
        ContentCategoryDTO categoryDTO = contentCategoryService.queryContentCategoryNodeList(parentId);
        return categoryDTO.getContentCategoryVOList();
    }

    @PostMapping("/create")
    @ResponseBody
    public ResultVO addContentNode(ContentVO contentVO) {
        ContentDTO contentDTO = new ContentDTO();
        contentDTO.setContentVO(contentVO);
        int row = contentService.addContent(contentDTO);
        return ResultVO.buildSuccessResult(0, "success", row);
    }

    @PostMapping("/delete")
    @ResponseBody
    public ResultVO deleteContent(String ids) {
        int row = contentService.deleteContent(ids);
        return ResultVO.buildSuccessResult(0, "success", row);
    }

    @PostMapping("/update")
    @ResponseBody
    public ResultVO updateContentNode(ContentVO contentVO) {
        ContentDTO contentDTO = new ContentDTO();
        contentDTO.setContentVO(contentVO);
        int row = contentService.updateContent(contentDTO);
        return ResultVO.buildSuccessResult(0, "success", row);
    }

    @GetMapping("/getContent")
    @ResponseBody
    public ResultVO getContent(Long id) {
        ContentDTO contentDTO = contentService.getContent(id);
        return ResultVO.buildSuccessResult(0, "success", contentDTO.getContentVO());
    }

    @GetMapping("/list")
    @ResponseBody
    public EasyUIVO queryContentList(Long categoryId, Integer page, Integer rows) {
        ContentDTO contentDTO = contentService.queryContentList(categoryId, page, rows);
        EasyUIVO easyUIVO = new EasyUIVO();
        easyUIVO.setTotal(contentDTO.getTotal());
        easyUIVO.setRows(contentDTO.getContentVOList());
        return easyUIVO;
    }
}
